package com.lc.cms.service.impl;

import com.lc.cms.dao.IUserDao;
import com.lc.cms.dto.*;
import com.lc.cms.model.UserModel;
import com.lc.cms.service.IUserService;
import com.lc.cms.utils.ResponseResult;
import com.lc.cms.utils.SystemConstant;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

@Log4j2
@Service
public class UserServiceImpl implements IUserService {

    private final IUserDao userDao;

    @Autowired
    public UserServiceImpl(IUserDao userDao) {
        this.userDao = userDao;
    }

    @Transactional
    @Override
    public ResponseResult create(UserModel user) {
        try {
            UserRoleDto userRole = new UserRoleDto();
            userRole.setUserId(user.getId());
            List<Integer> roleList = new ArrayList<>();
            roleList.add(SystemConstant.USER_ROLE_COMMON);
            userRole.setRoleList(roleList);
            int createUserRoleResult = userDao.createUserRole(userRole);
            if (createUserRoleResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            int createResult = userDao.create(user);
            if (createResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult update(UserModel user) {
        try {
            int updateResult = userDao.update(user);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult updatePassword(UserModel user) {
        try {
            int updateResult = userDao.updatePassword(user);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Transactional
    @Override
    public ResponseResult delete(int userId) {
        try {
            userDao.deleteUserDepartmentByUserId(userId);
            userDao.deleteUserRoleByUserId(userId);
            int deleteResult = userDao.delete(userId);
            if (deleteResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Transactional
    @Override
    public ResponseResult updateUserDepartment(UserDepartmentDto user) {
        try {
            userDao.deleteUserDepartmentByUserId(user.getUserId());
            int createUserDepartmentResult = userDao.createUserDepartment(user);
            if (createUserDepartmentResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Transactional
    @Override
    public ResponseResult updateUserRole(UserRoleDto user) {
        try {
            int deleteUserRoleResult = userDao.deleteUserRoleByUserId(user.getUserId());
            if (deleteUserRoleResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            int createUserRoleResult = userDao.createUserRole(user);
            if (createUserRoleResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult getListPagingByCondition(UserPagingParameterDto user) {
        try {
            int amount = userDao.getCountByCondition(user);
            List<UserDto> list = userDao.getListPagingByCondition(user);
            PagingResponseDto<UserDto> userResponse = new PagingResponseDto<>();
            userResponse.setAmount(amount);
            userResponse.setList(list);
            return ResponseResult.success(userResponse);
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }
}
